/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
var webjs = {
    isConnectedFacebook: false,
    callbackscript: "",
    isAcceptUnlike: false,
    isEndOfContest: false,
    aftersubmitlightbox: null,
    images:{
        unlike: ""
    },   
    submitQuestion: function()
    {        
        var error = 0;
        if(jQuery("#id_email").val() == ""){
            error = 1;
            jQuery("#id_email").addClass("error");
        }
        if(jQuery("#id_name").val() == ""){
            error = 1;
            jQuery("#id_name").addClass("error");
        }
        if(error == 1){
            return void(0);
        }
        
        jQuery.cookie("email",jQuery("#id_email").val(),{expires: 365});
        jQuery.cookie("name",jQuery("#id_name").val(),{expires: 365});
        jQuery.cookie("custom_answer",jQuery("#id_custom_answer").val(),{expires: 365});
        
        webjs.blockUIMessageBox(webjs.langs.please_wait);
        jQuery.post("?action=question", jQuery("#questionform").serialize(), function(data){
            jQuery.unblockUI();
            if(parseInt(data) == 1) {
                webjs.aftersubmitlightbox = new LightFace({
                    width: 420,
                    title: webjs.tostitle,
                    content: webjs.langs.thank_message,
                    buttons: [
                        {
                            title: "Del",
                            event: function() {
                                webjs.share();
                            },
                            color: 'blue'
                        },
                        {
                            title: webjs.langs.cancel,
                            event: function() {
                                webjs.aftersubmitlightbox.close();
                                jQuery("#questionform").html("");
                            },
                            color: 'blue'
                        }
                    ]
                }).open();
                jQuery(".lightfaceOverlay").fadeOut();
                webjs.questionbox.close();
//                jQuery("#questionform").html(webjs.langs.thank_message);
//                jQuery("#questionformtitle").css("display","none");
//                jQuery("div.lightfaceFooter .lightfaceblue input").val(webjs.langs.close);
            }
        });
    },
    tosbox: null,
    questionbox: null,   
    tostitle: "Dagens spørsmål, 18. desember",
    availableDay: 0,
    dateCheck: "on",
    langcode: "no",
    langs: {
        today_question: "Dagens spørsmål, ",
        dot_december: ". mars",
        cancel: "Avbryt",
        close: "Lukk",
        thank_message: "<center>Takk for ditt svar<br/><br/>Alle vinnere vil bli kontaktet på E-post.</center>"
    },
    
    showQuestionBox: function(myid){        
        if((myid != webjs.availableDay) && (webjs.dateCheck == "on")){            
            return void(0);
        }
        
        webjs.tostitle = webjs.langs.today_question + " " + myid + webjs.langs.dot_december;
        var randString = Math.random();
        var objectURL = '?action=question&day=' + myid + '&rnd=' + randString + "&lang=" + webjs.langcode + '&key=' + webjs.key + "&uid=" + webjs.uid;        
        if(webjs.questionbox != null) {
            webjs.questionbox.load(objectURL, webjs.tostitle);            
            webjs.questionbox.open();
        } else {            
            webjs.questionbox = new LightFace.Request({
                url: objectURL,
                width: 420,
                draggable: false,
                resetOnScroll: facebooktab.lightboxAutoAdjust,
                zIndex: 999,
                onOpen:function(){
                    if(!facebooktab.lightboxAutoAdjust){
                        facebooktab.resizeLightbox();
                    }
                },
                onComplete:function(){
                    if(!facebooktab.lightboxAutoAdjust){
                        facebooktab.resizeLightbox();
                    }
                    window.setTimeout("webjs.updateCookieData()", 500);                    
                },                
                buttons: [
                    {
                        title: webjs.langs.cancel,
                        event: function() {
                            this.close();
                            jQuery("#questionform").html("");
                        },
                        color: 'blue'
                    }
                ],
                request:{
                    evalScripts: true
                },
                title: webjs.tostitle
            }).open();
        }
        jQuery("div.lightfaceFooter .lightfaceblue input").val(webjs.langs.cancel);
        return void(0);
    },
    updateCookieData: function()
    {
        // set cookie data if detect
        //jQuery.cookie("email","phamhphuc@gmail.com",{expires: 365});
        if(jQuery.cookie("email") != null) {
            jQuery("#id_email").val(jQuery.cookie("email"));
        }  
        if(jQuery.cookie("name") != null) {
            jQuery("#id_name").val(jQuery.cookie("name"));
        }
        if(jQuery.cookie("custom_answer") != null) {
            jQuery("#id_custom_answer").val(jQuery.cookie("custom_answer"));
        }
    },
    showTOSWindow: function() {
        var randString = Math.random();
        var objectURL = '?action=tos&key=' + webjs.key + "&uid=" + webjs.uid;
        if(webjs.tosbox == null) {
            webjs.tosbox = new LightFace.Request({
                url: objectURL,
                width: 420,
                draggable: false,
                resetOnScroll: facebooktab.lightboxAutoAdjust,
                zIndex: 999,
                onOpen:function(){
                    if(!facebooktab.lightboxAutoAdjust){
                        facebooktab.resizeLightbox();
                    }
                },
                onComplete:function(){
                    if(!facebooktab.lightboxAutoAdjust){
                        facebooktab.resizeLightbox();
                    } else {
//                        window.setTimeout("facebooktab.likelightbox._position()", 1000);
                    }
                },
                buttons: [
                    {
                        title: webjs.langs.close,
                        event: function() {
                            this.close();
                        },
                        color: 'blue'
                    }
                ],
                request:{
                    evalScripts: true
                },
                title: "TOS"
            }).open();
        } else {
            webjs.tosbox.open();
        }
        return void(0);
    },
    lockScreen: function(id) {
        // calculate the doucument width
        var w = jQuery(document).width();
        var neww = (w/2) - (520/2) - 10;
        jQuery.blockUI({message: jQuery("#" + id),
            css: {
                top: "170px",
                width: "520px",
                left: neww + "px"
            }
        });
    },
    blockUIMessageBox: function(message) {
        jQuery.blockUI({message: message,
            css: {
            border: 'none',
            padding: '15px',
            backgroundColor: '#000',
            '-webkit-border-radius': '10px',
            '-moz-border-radius': '10px',
            opacity: .5,
            color: '#fff'
        }});
        jQuery('.blockOverlay').attr('title','Click to unblock').click(jQuery.unblockUI);
    },
    shareData: {},
    
    setFacebookShareData: function(shareData)
    {
        webjs.shareData = shareData;
    },
    
    share: function(){
        var shareData = webjs.shareData;
        FB.ui(
                {
                    method: 'feed',
                    picture: shareData.picture,
                    name: shareData.name,
                    caption: shareData.caption,
                    link:shareData.url,
                    description: shareData.description,
                    action_links: [
                        {text: shareData.actionText, href: shareData.url}
                    ]
                },
                function(response) {
                    if (response && response.post_id) {
                    } else {
                    }
                }
            );
    },
    sendRequestViaMultiFriendSelector: function(title, message) {
      FB.ui({method: 'apprequests',
            title: title,
            message: message
      }, function(response) {});
    }
};

